using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using XLua;

public class XLuaManager : MonoBehaviour
{
    private LuaEnv luaEnv;
    private Action luaStart;
    private Action luaUpdate;

    void Awake()
    {
        luaEnv = new LuaEnv();
        luaEnv.AddLoader(CustomLoader);
        luaEnv.DoString("require 'Main'");
        luaStart = luaEnv.Global.Get<Action>("LuaStart");
        luaUpdate = luaEnv.Global.Get<Action>("LuaUpdate");
    }

    private byte[] CustomLoader(ref string filepath)
    {
        return File.ReadAllBytes(Application.dataPath + "/Lua/" + filepath + ".lua");
    }

    // Start is called before the first frame update
    void Start()
    {
        luaStart?.Invoke();
    }

    // Update is called once per frame
    void Update()
    {
        luaUpdate?.Invoke();
    }
}
